﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Tanks.Contracts;

namespace TanksGame
{
    class GameScreen : IGameObject
    {
        private IMapReader _mapReader;
        private IContentReader _contentReader;
        private Tanks _tanksGame;
        private Texture2D tree;
        private Texture2D brick;
        private Texture2D concrete;
        private Texture2D head;
        private Texture2D water;
        private Texture2D ice;
        private Texture2D birth;
        private Texture2D tank;
        private SpriteFont spriteFont;
        private Map _map;
        List<AnimatedSprite> _sprites = new List<AnimatedSprite>();

        public GameScreen(Tanks tanksGame, IContentReader contentReader)
        {
            _tanksGame = tanksGame;
            _contentReader = contentReader;

        }

        private List<AnimatedSprite> MapToMapItemsList(Map levelMap)
        {
            foreach (var mapItem in levelMap.MapItems)
            {
                int _numberOfFrames = 1;
                Texture2D temp = null;
                switch (mapItem.ItemType)
                {
                    case MapItemType.Brick:
                        temp = brick;
                        break;
                    case MapItemType.Concrete:
                        temp = concrete;
                        break;
                    case MapItemType.Head:
                        temp = head;
                        break;
                    case MapItemType.Ice:
                        temp = ice;
                        break;
                    case MapItemType.Water:
                        temp = water;
                        _numberOfFrames = 2;
                        break;
                    case MapItemType.Wood:
                        temp = tree;
                        break;
                }

                _sprites.Add(new AnimatedSprite(mapItem.ItemType, temp, mapItem.X, mapItem.Y,
                                                Map.MapItemSize[mapItem.ItemType], _numberOfFrames,700));
            }
            _sprites.Add(new Tank(MapItemType.EnemyTank, tank, 2, 2, 2, 2, 100, 0));
            _sprites.Add(new AnimatedSprite(MapItemType.Empty,birth, 0, 0, 2, 6, 200));
            return _sprites;
        }

        public static Dictionary<Directions, float> DirectionToRadians
        {
            get
            {
                return new Dictionary<Directions, float>
                           {
                               {Directions.Up, 0f},
                               {Directions.Left, (float) (-Math.PI/2)},
                               {Directions.Down, (float) (-Math.PI)},
                               {Directions.Right, (float) (-3*Math.PI/2)}
                           };

            }
        }

        public  void LoadContent()
        {
            tree = _contentReader.GetTexture("Tree");
            brick = _contentReader.GetTexture("Brick");
            concrete = _contentReader.GetTexture("Concrete");
            water = _contentReader.GetTexture("Water");
            ice = _contentReader.GetTexture("Ice");
            head = _contentReader.GetTexture("Head");
            birth = _contentReader.GetTexture("Birth");
            tank = _contentReader.GetTexture("Tanks");
            spriteFont = _contentReader.GetFont("font");
            _map = _contentReader.GetMap("3");
            _sprites = MapToMapItemsList(_map);
        }

        public void Update(GameTime gameTime, KeyboardState keyboardState)
        {
            foreach (var sprite in _sprites)
            {
                if (sprite._numberOfFrames>1)
                sprite.Update(gameTime, keyboardState);
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
           foreach (var sprite in _sprites.OrderBy(x=>x.MapItemType))
            {   
               if (sprite.Texture!=null)
                sprite.Draw(spriteBatch);
               
            }
        }
    }
}
